package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.*;
import com.cskaoyan.market.service.AdminOrderService;
import com.cskaoyan.market.util.MybatisUtils;
import com.cskaoyan.market.vo.MarketOrderDetailVo;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.util.List;

public class AdminOrderServiceImpl implements AdminOrderService {


    @Override
    public List<MarketOrder> list(String page, String limit, String sort, String order, String start, String end) {
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        List<MarketOrderExample.Criteria> oredCriteria = marketOrderExample.getOredCriteria();
        marketOrderExample.setOrderByClause(sort +" "+order);

        PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(limit));

        return MybatisUtils.getSession(true).getMapper(MarketOrderMapper.class).selectByExample(marketOrderExample);
    }

    @Override
    public MarketOrderDetailVo detail(String id) {
        SqlSession session = MybatisUtils.getSession();
        //订单
        MarketOrderMapper marketOrderMapper = session.getMapper(MarketOrderMapper.class);
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(Integer.parseInt(id));


        //OrderGoods
        MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria orderGoodsExampleCriteria = marketOrderGoodsExample.createCriteria();
        orderGoodsExampleCriteria.andOrderIdEqualTo(Integer.parseInt(id));

        List<MarketOrderGoods> marketOrderGoods = session.getMapper(MarketOrderGoodsMapper.class).selectByExample(marketOrderGoodsExample);


        //用户


        MarketUser marketUser = session.getMapper(MarketUserMapper.class).selectByPrimaryKey(marketOrder.getUserId());

        //Vo
        return  new MarketOrderDetailVo(marketOrderGoods,marketUser,marketOrder);
    }

    @Override
    public int update(MarketOrder marketOrder) {
        return MybatisUtils.getSession(true).getMapper(MarketOrderMapper.class).updateByPrimaryKeySelective(marketOrder);
    }

    @Override
    public int delete(MarketOrder marketOrder,List<Integer> orderStatusList) {
        SqlSession session = MybatisUtils.getSession();
        MarketOrderMapper mapper =     session.getMapper(MarketOrderMapper.class);
        marketOrder = mapper.selectByPrimaryKey(marketOrder.getId());
        if(orderStatusList.contains(Integer.valueOf(marketOrder.getOrderStatus())))
        {return 0;}
        int effectRows = mapper.deleteByPrimaryKey(marketOrder.getId());
        session.commit();
        session.close();
        return effectRows;
    }

    @Override
    public int updateComment(MarketComment marketComment) {
        SqlSession session = MybatisUtils.getSession();
        MarketCommentMapper mapper = session.getMapper(MarketCommentMapper.class);
        marketComment = mapper.selectByPrimaryKey(marketComment.getId());
        String adminContent = marketComment.getAdminContent();
        if(adminContent==null){
            marketComment.setUpdateTime(LocalDateTime.now());
            return mapper.updateByPrimaryKeySelective(marketComment);
        }
        return -1;
    }


}
